package com.yf1706.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yf1706.bean.CheckComputeRule;
import com.yf1706.bean.CheckRule;
import com.yf1706.service.CheckComputeRuleService;
import com.yf1706.service.checkRuleService;
import com.yf1706.vo.checkRuleVO;
import com.yf1706.vo.upCheckRuleVO;

@Controller
@RequestMapping("rule")
public class checkRuleController extends BaseController{
	@Autowired
	private checkRuleService crs;
	@Autowired
	private CheckComputeRuleService ccrs;
	
	@RequestMapping("toCheckRule")
	public String checkFlush() {
		System.out.println("进入新增考勤规则界面");
		return "hyn/addCheckRule";
	}
	//查询全部考勤规则信息
	@RequestMapping("getcheckRule")
	@ResponseBody
	public Map<String, Object> getCheckRulelist(int page,int rows){
		Page<CheckRule> pages = PageHelper.startPage(page, rows);
        List<CheckRule> list= crs.getcheckRuleList();
        Map<String, Object> map = new HashMap<String, Object>();
        long total = pages.getTotal();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	//查询全部考勤规则信息
		@RequestMapping("getcheckRules")
		@ResponseBody
		public List<CheckRule> getCheckRulelist(){
	        List<CheckRule> list= crs.getcheckRuleList();
			return list;
		}
		//查询全部考勤规则信息
		@RequestMapping("getcheckRule2")
		@ResponseBody
		public List<CheckRule> getcheckRule2(String ruleId){
			System.out.println(ruleId);
			int id1=Integer.parseInt(ruleId);
	        List<CheckRule> list= crs.getcheckRuleList(id1);
			return list;
		}
		
	//新增计算规则信息
	@RequestMapping("addCheckRule")
	@ResponseBody
	public void addCheckRules(HttpServletRequest request, HttpServletResponse response){
		 String jsondata=request.getParameter("jsondata");
		 System.out.println(jsondata);
		 List<checkRuleVO> list = JSONObject.parseArray(jsondata, checkRuleVO.class);
		list.get(0).setMaker(getSessionUserName(request));
		 crs.addCheckRule(list.get(0));
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	//修改计算规则信息
	@RequestMapping("updateCheckRule")
	@ResponseBody
	public void updateCheckRules(HttpServletRequest request, HttpServletResponse response){
		 String jsondata=request.getParameter("jsondata");
		 List<upCheckRuleVO> list = JSONObject.parseArray(jsondata, upCheckRuleVO.class);
		 crs.updateCheckRule(list.get(0));
         
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	//删除假种信息
	@RequestMapping("deleteCheckRule")
	@ResponseBody
	public void deleteCheckRules(HttpServletRequest request, HttpServletResponse response){
		 String jsondata=request.getParameter("jsondata");
		 List<Integer> list = (List<Integer>) JSONObject.parse(jsondata);	
       for (Integer o : list) {
    	   crs.deleteCheckRule(o.intValue());
	}
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	//模糊查询考勤规则信息
	@RequestMapping("serchCheckRule")
	@ResponseBody
	public List<CheckRule> serchCheckRule(String ruleName){
		System.out.println(ruleName);
		 List<CheckRule>  list= crs.serchcheckRuleList(ruleName);
		 return list;
	}
	
	//查询全部计算规则信息
	@RequestMapping("getComputeRule")
	@ResponseBody
	public Map<String, Object> 	getComputeRulelist(int page,int rows){
		Page<CheckComputeRule> pages = PageHelper.startPage(page, rows);
        List<CheckComputeRule> list= ccrs.getComputeRulelist();
        Map<String, Object> map = new HashMap<String, Object>();
        long total = pages.getTotal();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	//查询全部计算规则信息
		@RequestMapping("getComputeRules2")
		@ResponseBody
		public List<CheckComputeRule>	getComputeRulelist(){
	        List<CheckComputeRule> list= ccrs.getComputeRulelist();
			return list;
		}
	//新增计算规则信息
	@RequestMapping("addComputeRule")
	@ResponseBody
	public void addComputeRules(HttpServletRequest request, HttpServletResponse response){
		 String jsondata=request.getParameter("jsondata");
		 List<CheckComputeRule> list = JSONObject.parseArray(jsondata, CheckComputeRule.class);
        list.get(0).setMaker(getSessionUserName(request));
		 ccrs.addComputeRule(list.get(0));
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	//修改计算规则信息
	@RequestMapping("updateComputeRule")
	@ResponseBody
	public void updateComputeRules(HttpServletRequest request, HttpServletResponse response){
		 String jsondata=request.getParameter("update_jsondata");
		 List<CheckComputeRule> list = JSONObject.parseArray(jsondata, CheckComputeRule.class);
		 list.get(0).setUpdateEmp(getSessionUserName(request));
		 ccrs.updateComputeRule(list.get(0));
         
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	//删除假种信息
	@RequestMapping("deletecomputeRule")
	@ResponseBody
	public void deletecomputeRules(HttpServletRequest request, HttpServletResponse response){
		 String jsondata=request.getParameter("huo_rowData");
		 List<Integer> list = (List<Integer>) JSONObject.parse(jsondata);	
       for (Integer o : list) {
    	   ccrs.deletecomputeRule(o.intValue());
	}
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	//模糊查询班次信息
	@RequestMapping("serchComputeRule")
	@ResponseBody
	public List<CheckComputeRule> serchComputeRule(String computeName){
		System.out.println(computeName);
		 List<CheckComputeRule>  list= ccrs.getSerchComputeRuleList(computeName);
		 return list;
	}
			
}
